package protocol.work;

import java.util.List;
import java.util.Scanner;

import org.apache.commons.logging.Log;

import protocol.common.entity.HttpConnConfig;
import protocol.common.util.CJLog;
import protocol.common.util.ConfigLoader;
import protocol.data.services.ConfigLoaderServices;
import protocol.data.util.JedisPoolUtil;
import protocol.data.util.SqlMapKit;
import protocol.work.connection.ConnectionFactory;
import protocol.work.connection.ConnectionManager;

public class SystemManager {
	protected Log logger = CJLog.getLogConsole();
	
	private ConfigLoaderServices configLoaderServices;
	
	private static SystemManager system;
	
	private SystemManager() {
		this.configLoaderServices = new ConfigLoaderServices();
		ConfigLoader.init("SYS-.*\\.properties");         //加载配置文件
		SqlMapKit.init();
		JedisPoolUtil.init();
	}
	
	public static SystemManager getSystemInstance(){
		if(system == null){
			system = new SystemManager();
		}
		return system;
	}
	

	private void initWebServices(HttpConnConfig httpConnConfig) throws Exception{
//		if(httpConnConfig.getApiGetReportType() == 2 || httpConnConfig.getApiGetDeliverType() == 2){
//			String webRootPath = SystemManager.class.getResource("").getPath().replace("%20", " ");
//			String port = httpConnConfig.getWebListenerParameters().get("Port").toString();
//			Connector connector = new SelectChannelConnector();
//			connector.setPort(Integer.parseInt(port));
//			
//			Server server = new Server();
//			server.setConnectors(new Connector[]{connector});
//			InetAddress addr = InetAddress.getLocalHost();
//			String ip=addr.getHostAddress().toString();//获得本机IP
//			String webContent = httpConnConfig.getWebListenerParameters().get("WebContent").toString();
//			WebAppContext wac = new WebAppContext(server,webRootPath,"/"+webContent);
//			
//			if(httpConnConfig.getApiGetReportType() == 2){
//				wac.addServlet(HttpReadReportListener.class,"/"+httpConnConfig.getApiGetReportUrl());
//				logger.info("<系统信息> 回执服务器建立成功，回执地址：http://"+ip+":"+port+"/"+webContent+"/"+httpConnConfig.getApiGetReportUrl());
//			}
//			
//			if(httpConnConfig.getApiGetDeliverType() == 2){
//				wac.addServlet(HttpReadDelivrdListener.class,"/"+httpConnConfig.getApiGetDeliverUrl());
//				logger.info("<系统信息> 回执服务器建立成功，上行地址：http://"+ip+":"+port+"/"+webContent+"/"+httpConnConfig.getApiGetDeliverUrl());
//			}
//			server.start();
//		}
	}
	
	public void Start(){
		List<HttpConnConfig> connConfigLists = configLoaderServices.readConfigFromPropertiesFile("classpath:HTTP-ChannelConfig.properties");
		for(HttpConnConfig connConfig : connConfigLists){
			ConnectionManager connManager = ConnectionFactory.createQueryConnection(connConfig);
		}
	}
	
	
	
	public static void main(String[] args) throws Exception{
		Scanner scanner = new Scanner(System.in);
		try {
			StringBuilder systemInfo = new StringBuilder();
			systemInfo.append("┏━━━━━━━━━━━━━━━━━━━━┓").append("\n");
			systemInfo.append("    系统信息                                                                               ").append("\n");
			systemInfo.append("┣━━━━━━━━━━━━━━━━━━━━┫").append("\n");
			systemInfo.append("    系统名称 : HTTP网关接口                                           ").append("\n");
			systemInfo.append("┣━━━━━━━━━━━━━━━━━━━━┫").append("\n");
			systemInfo.append("    版权所有 : 南京惠承通信息技术有限公司        ").append("\n");
			systemInfo.append("┣━━━━━━━━━━━━━━━━━━━━┫").append("\n");
			systemInfo.append("    开发厂商 : 南京惠承通信息技术有限公司        ").append("\n");
			systemInfo.append("┣━━━━━━━━━━━━━━━━━━━━┫").append("\n");
			systemInfo.append("    修改时间 : 2015年05月24日                                     ").append("\n");
			systemInfo.append("┣━━━━━━━━━━━━━━━━━━━━┫").append("\n");
			systemInfo.append("    系统版本 : V 1.0.0                    ").append("\n");
			systemInfo.append("┗━━━━━━━━━━━━━━━━━━━━┛").append("\n");
			System.out.println(systemInfo);
			Thread.currentThread().setName("ConnectionMain");
			SystemManager.getSystemInstance().Start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		scanner.close();
	}
	
}
